Ext.namespace('Wlanauth');

Wlanauth.BaseFormPanel=Ext.extend(Ext.form.FormPanel,{
	editMode:'create',
	actionClass:'',
	entityName:'entity',
	frame:false,
	border:false,
	buttonAlign:'center',
	constructor:function(config){
		Ext.apply(this,config);
	      
        Wlanauth.BaseFormPanel.superclass.constructor.call(this,{
        	waitMsgTarget:true
        	 
        });
        this.on({
			  'actionfailed': function(form, action) {
		
				var title = (action.type == 'load') ? '取数失败' : (action.type == 'submit') ? '提交失败' : '失败';
				var msg,login;
				if (action.failureType == Ext.form.Action.CLIENT_INVALID) {
					msg = '您输入的数据有误、或必填项没填，请检查！';
				}
				else if (action.failureType == Ext.form.Action.LOAD_FAILURE) {
					if (action.result.success) {
						msg = "服务器返回的数据格式不正确！";
					}else {
						msg = action.result.data;
						if (!msg) {
							msg = '服务器返回的数据格式不正确！';
						}
					}
					login = action.result.login;
				} else if (action.failureType == Ext.form.Action.CONNECT_FAILURE) {
					msg = '通讯失败，请检查网络是否正常！';
					msg += '<br><br>' + action.response.status + ': ' + action.response.statusText; 
				} else if (action.failureType == Ext.form.Action.SERVER_INVALID) {
					msg = action.result.data;
					if (!msg) {
						msg = '您输入的数据有误、或必填项为空，请检查！';
					}
					login = action.result.login;
				}	else {
					msg = action.result.data;
					if (!msg) {
						// 不知何故，提交后服务端返回{success:false, errMsg:'服务端保存失败！'}时，failureType没赋值。
						// 但如果返回{success:false, errors:{}, errMsg:'服务端保存失败！'}，则failureType为server。
						// 统一采用后一种格式。
						msg = '服务端响应数据的格式不正确：' + action.response.responseText;
					}
				}
				Ext.Msg.alert(title, msg, function(buttonId, text, opt) {
					if (login) {
						ZZExt.ApplicationContext.login();
					}
				});
		  	}
		});
        
        this.addEvents('actionSuccess');
      },
      setMode:function(status){
      	this.editMode=status;
      	this.getForm().reset();
      },
      isValid:function(form){
      	return form.isValid();
      },
      doSubmit:function(){
      	var _url = this.actionClass+'!save.action';
      	if(this.editMode.toUpperCase() == 'UPDATE')
      		_url = this.actionClass+'!update.action';
      	var _form = this.getForm();
      	if(this.isValid(_form)){
      		_form.submit({
      			url:_url,
      			scope:this,
      			waitMsg:'正在保存数据...',
      			success:function(form,action){
      				this.fireEvent('actionSuccess',form,action);
      			}
      		});
      	}
      },
      loadDataById:function(id){
    	this.setMode('update');  
      	var form = this.getForm();
      	form.entityName = this.entityName;
      	var entityId = (this.entityName==null?'id':this.entityName+'.id');
      	form.load({
      		waitMsg:'正在加载数据...',
      		url:this.actionClass+'!edit.action?'+entityId+'='+id,
      		scope:this,
      		success:this.onLoadDataSuccess
      	});
      },
      onLoadDataSuccess:function(form,action){
      	//doMethod
      }
});